﻿using System;
using System.Windows.Forms;
using ECP.Service.Utilities;
using ECP.Model;
using ECP.Utilities;
using DevExpress.XtraEditors;

namespace ECP.Client.Sequence
{
    public partial class SequenceEditFrm : FormBase
    {
        private BaseSequenceEntity SequenceEntity;
        public string EntityID;

        public SequenceEditFrm()
        {
            this.EntityID = string.Empty;
            this.InitializeComponent();
        }

        public SequenceEditFrm(string id)
            : this()
        {
            this.EntityID = id;
        }

        private bool CheckInput()
        {
            this.txtFullName.Text = this.txtFullName.Text.TrimEnd(new char[0]);
            if (this.txtFullName.Text.Trim().Length == 0)
            {
                XtraMessageBox.Show(AppMessage.Format(AppMessage.MSG0007, new string[] { AppMessage.MSG9978 }), AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                this.txtFullName.Focus();
                return false;
            }
            long result = 0L;
            if (this.txtSequence.Text.Trim().Length == 0)
            {
                XtraMessageBox.Show(AppMessage.Format(AppMessage.MSG0007, new string[] { "增序列" }), AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                this.txtSequence.Focus();
                return false;
            }
            if (!long.TryParse(this.txtSequence.Text, out result))
            {
                XtraMessageBox.Show(AppMessage.Format(AppMessage.MSG0030, new string[] { "增序列" }), AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                this.txtSequence.Focus();
                return false;
            }
            long num2 = 0L;
            if (this.txtDegression.Text.Trim().Length == 0)
            {
                XtraMessageBox.Show(AppMessage.Format(AppMessage.MSG0007, new string[] { "减序列" }), AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                this.txtDegression.Focus();
                return false;
            }
            if (!long.TryParse(this.txtDegression.Text, out num2))
            {
                XtraMessageBox.Show(AppMessage.Format(AppMessage.MSG0030, new string[] { "减序列" }), AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                this.txtDegression.Focus();
                return false;
            }
            if (result <= num2)
            {
                XtraMessageBox.Show(AppMessage.Format(AppMessage.MSG0025, new string[] { "减序列", "增序列" }), AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                this.txtSequence.Focus();
                return false;
            }
            int num3 = 0;
            if (this.txtStep.Text.Trim().Length == 0)
            {
                XtraMessageBox.Show(AppMessage.Format(AppMessage.MSG0007, new string[] { "步调" }), AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                this.txtStep.Focus();
                return false;
            }
            if (!int.TryParse(this.txtStep.Text, out num3))
            {
                XtraMessageBox.Show(AppMessage.Format(AppMessage.MSG0030, new string[] { "步调" }), AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                this.txtSequence.Focus();
                return false;
            }
            return true;
        }

        private BaseSequenceEntity GetEntity(BaseSequenceEntity sequenceEntity)
        {
            sequenceEntity.FullName = this.txtFullName.Text;
            sequenceEntity.Prefix = this.txtPrefix.Text;
            sequenceEntity.Separate = this.txtSeparate.Text;
            sequenceEntity.Sequence = long.Parse(this.txtSequence.Text);
            sequenceEntity.Degression = long.Parse(this.txtDegression.Text);
            sequenceEntity.Step = int.Parse(this.txtStep.Text);
            sequenceEntity.Description = this.txtDescription.Text;
            return sequenceEntity;
        }

        private void FormOnLoad()
        {
            base.Localization(this);
            this.SequenceEntity = ServiceManager.Instance.SequenceService.GetEntity(base.UserInfo, this.EntityID);
            this.ShowEntity(this.SequenceEntity);
            base.ActiveControl = this.txtFullName;
            this.txtFullName.Focus();
        }

        private bool SaveEntity()
        {
            string statusCode = string.Empty;
            string statusMessage = string.Empty;
            this.GetEntity(this.SequenceEntity);
            ServiceManager.Instance.SequenceService.Update(base.UserInfo, this.SequenceEntity, out statusCode, out statusMessage);
            if (statusCode == StatusCode.OKUpdate.ToString())
            {
                if (BaseSystemInfo.ShowInformation)
                {
                    XtraMessageBox.Show(statusMessage, AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                base.Changed = true;
                return true;
            }
            XtraMessageBox.Show(statusMessage, AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            if (statusCode == StatusCode.ErrorFullNameExist.ToString())
            {
                this.txtFullName.SelectAll();
                this.txtFullName.Focus();
            }
            return false;
        }

        private void ShowEntity(BaseSequenceEntity sequenceEntity)
        {
            this.txtFullName.Text = sequenceEntity.FullName;
            this.txtPrefix.Text = sequenceEntity.Prefix;
            this.txtSeparate.Text = sequenceEntity.Separate;
            this.txtSequence.Text = sequenceEntity.Sequence.ToString();
            this.txtDegression.Text = sequenceEntity.Degression.ToString();
            this.txtStep.Text = sequenceEntity.Step.ToString();
            this.txtDescription.Text = sequenceEntity.Description;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            base.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.CheckInput())
            {
                this.Cursor = Cursors.WaitCursor;
                try
                {
                    if (this.SaveEntity())
                    {
                        base.DialogResult = System.Windows.Forms.DialogResult.OK;
                        base.Close();
                    }
                }
                catch (Exception exception)
                {
                    base.ProcessException(exception);
                }
                finally
                {
                    this.Cursor = Cursors.Default;
                }
            }
        }

        private void SequenceEditFrm_KeyDown(object sender, KeyEventArgs e)
        {
            string str2;
            if ((((str2 = e.KeyCode.ToString()) != null) && ((str2 == "Enter") || (str2 == "Return"))) && ((base.ActiveControl is TextBox) && !((TextBox)base.ActiveControl).Multiline))
            {
                SendKeys.Send("{TAB}");
            }
        }

        private void SequenceEditFrm_Load(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            try
            {
                this.FormOnLoad();
            }
            catch (Exception exception)
            {
                base.ProcessException(exception);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
    }
}